﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;

namespace LibraryUtility.Common
{
    /// <summary>
    /// ClassName:Data Config Handler.
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    public class DataConfigHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler 成员

        /// <summary>
        /// Creates a configuration section handler.
        /// </summary>
        /// <param name="parent">Parent object.</param>
        /// <param name="configContext">Configuration context object.</param>
        /// <param name="section">Section XML node.</param>
        /// <returns>The created section handler object.</returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            List<DataConfigItem> list = new List<DataConfigItem>();
            if (parent != null)
                list = (List<DataConfigItem>)parent;

            NameValueCollection settings = null;
            try
            {
                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            }
            catch
            {
                settings = null;
            }


            if (settings == null)
            {
                ;//为空,读取默认值
            }
            else
            {
                DataConfigItem item = null;
                for (int i = 0; i < settings.Count; i++)
                {
                    item = new DataConfigItem();
                    item.Key = settings.GetKey(i);
                    item.Value = settings.Get(i);
                    list.Add(item);
                }
            }
            return list;
        }

        #endregion
    }
    /// <summary>
    /// ClassName:Data Config Items.
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    class DataConfigItem
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataConfigItem"/> class.
        /// </summary>
        public DataConfigItem()
        {
            ;
        }
        /// <summary>
        /// _Key is Files.
        /// </summary>
        private string _key;
        /// <summary>
        /// Gets or sets the key.
        /// </summary>
        /// <value>The key.</value>
        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }

        /// <summary>
        /// _value is Files.
        /// </summary>
        private string _value;
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

    }
}
